import locale from '../../locale/locale'
import fmtListDom from './fmtListDom'
import fmtListConfirm from './fmtlistConfirm'
import echoFns from './fmtListEcho'
import Store from '../../store'
import otherFmt from '../../locale/zh_fmt'
import { update } from '../../global/format';
import luckysheetMoreFormat from "../moreFormat"
const sfmt = {
  curType: '自动',
  curCell: null,
  getCurCell() {
    const selectSave = Store.luckysheet_select_save[0]
    const curCell = Store.flowdata[selectSave.row[0]][selectSave.column[0]]
    sfmt.curCell = curCell
  },
  initFmtLeftList() {
    const _locale = locale()
    const locale_format = _locale.format
    const locale_sfmt = _locale.sfmt
    setTimeout(() => {
      sfmt.initEvent()
      if (sfmt.curCell?.ct?.fa) {
        const fa = sfmt.curCell.ct.fa
        const t = sfmt.curCell.ct.t

        for (let index = 0; index < locale_sfmt.length; index++) {
          let item = locale_sfmt[index]
          if (item.reg?.test(fa)) {
            $('#fmtContainerList').find('.curfmt').removeClass('curfmt')
            $(`#fmtContainerList > div:nth-child(${index + 1})`).addClass('curfmt')
            sfmt.showRightContainer(locale_sfmt[index].text)
            break
          }
          if (t === 'd' && luckysheetMoreFormat[item?.regArr]?.find(i => i.value === fa)) {
            $('#fmtContainerList').find('.curfmt').removeClass('curfmt')
            $(`#fmtContainerList > div:nth-child(${index + 1})`).addClass('curfmt')
            sfmt.showRightContainer(locale_sfmt[index].text)
            break
          }
        }
      } else {
        $('#fmtContainerList').find('.curfmt').removeClass('curfmt')
        $('#fmtContainerList > div:nth-child(1)').addClass('curfmt')
        sfmt.showRightContainer(locale_sfmt[0].text)
      }

    })

    return `<div id="fmtContainerList">${locale_sfmt
      .map(
        (i, index) =>
          `<div class="${index == 0 ? 'curfmt' : ''}"" itemvalue="${i.value}" itemname="${i.text}">${i.text}</div>`
      )
      .join('')}</div>`
  },

  showRightContainer(type) {
    const _locale = locale()
    const locale_format = _locale.format
    const locale_sfmt = _locale.sfmt

    // 获取第一个单元格对象
    const selectSave = Store.luckysheet_select_save[0]
    const curCell = Store.flowdata[selectSave.row[0]][selectSave.column[0]]
    console.log(curCell)

    for (let index = 0; index < locale_sfmt.length; index++) {
      let item = locale_sfmt[index]
      if (item.text === type) {
        try {
          fmtListDom[item.fnName](curCell)
          echoFns[item.fnName](sfmt.curCell)
        } catch (err) {
          console.log(err);
        }
        break
      }
    }

    setTimeout(() => {
      // input number 小数位数不能小于0
      $('#setfmtRightContainer input[type="number"]').off('change').on('change', function (e) {
        if ($(e.target).val() < 0) {
          alert('小数位数不能小于0')
          $(e.target).val(0)
          return
        }
      })
    })


  },
  confirm(itemname) {
    const _locale = locale()
    const locale_format = _locale.format
    const locale_sfmt = _locale.sfmt

    for (let index = 0; index < locale_sfmt.length; index++) {
      let item = locale_sfmt[index]
      if (item.text === itemname) {
        fmtListConfirm[item.fnName]()
      }
    }
  },
  initEvent() {

    $('#fmtContainerList')
      .off('click.fmt')
      .on('click.fmt', function (e) {
        const itemname = $(e.target).attr('itemname')
        if (itemname) {
          $('#fmtContainerList').find('.curfmt').removeClass('curfmt')
          $(e.target).addClass('curfmt')
          sfmt.showRightContainer(itemname)
        }
      })
  }
}
export default sfmt
